package com.group.project.hrms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.group.project.hrms.entity.FrontLineVacationRecord;
import com.group.project.hrms.entity.ResultEntity.ResultEntity;
import com.group.project.hrms.service.IFrontLineVacationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class FrontLineVacationRecordController {

    @Autowired
    private IFrontLineVacationRecordService iFrontLineVacationRecordService;


    /**
     * 根据用户传入的时间段，获取该时间段内的一线生产员工请假详情
     * @param map 前端传入的日期数据
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doGetFrontLineVacationRecordsByRangeDate")
    @ResponseBody
    public ResultEntity doGetFrontLineVacationRecordsByRangeDate(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        if(null != map){
            Object dateArray = map.get("rangeDate");
            if(null != dateArray){
                JSONArray array = JSONArray.parseArray(JSON.toJSONString(dateArray));
                String dateStart = array.getString(0);
                String dateEnd = array.getString(1);
                try{
                    List<FrontLineVacationRecord> resultList = iFrontLineVacationRecordService.selectFrontLineVacationRecordByRangeCreateDate(dateStart, dateEnd);
                    resultEntity.setCode("S");
                    resultEntity.setMsg("Success");
                    resultEntity.setDataList(resultList);
                    return resultEntity;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("选择的日期不正确");
                return resultEntity;
            }
        }else {
            resultEntity.setCode("F");
            resultEntity.setMsg("请求参数错误");
            return resultEntity;
        }
        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        return resultEntity;
    }
}
